package Views;

import Controllers.CustomerController;
import Controllers.LocationController;
import Models.Customer;
import Models.EmptyValueException;
import Models.Location;
import Views.TableModels.LocationTableModel;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;

public class CustomerPanel extends ViewAbstract<Models.Customer>
{
	public CustomerPanel(SearchableTabbedPane parent)
	{
		initComponents();
		this.controller = new CustomerController();
		this.tabbedPane = parent;
		locationTable.getSelectionModel().addListSelectionListener(this);
		locationTable.addMouseListener(new java.awt.event.MouseAdapter(){
			@Override
			public void mouseClicked(java.awt.event.MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(locationTable.getSelectedRowCount() == 1)
					{
						showLocationBActionPerformed(null);
					}
				}
			}
		});
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        customerNoL = new javax.swing.JLabel();
        nameL = new javax.swing.JLabel();
        addressL = new javax.swing.JLabel();
        zipCodeL = new javax.swing.JLabel();
        cityL = new javax.swing.JLabel();
        cvrL = new javax.swing.JLabel();
        phoneNoL = new javax.swing.JLabel();
        customerNoTF = new javax.swing.JTextField();
        nameTF = new javax.swing.JTextField();
        addressTF = new javax.swing.JTextField();
        zipCodeTF = new javax.swing.JTextField();
        cityTF = new javax.swing.JTextField();
        cvrTF = new javax.swing.JTextField();
        phoneNoTF = new javax.swing.JTextField();
        searchTF = new javax.swing.JTextField();
        searchB = new javax.swing.JButton();
        addLocationB = new javax.swing.JButton();
        removeLocationB = new javax.swing.JButton();
        primaryLocationB = new javax.swing.JButton();
        showLocationB = new javax.swing.JButton();
        showAllB = new javax.swing.JButton();
        newB = new javax.swing.JButton();
        editB = new javax.swing.JButton();
        deleteB = new javax.swing.JButton();
        cancelB = new javax.swing.JButton();
        saveB = new javax.swing.JButton();
        Sep1 = new javax.swing.JSeparator();
        Sep2 = new javax.swing.JSeparator();
        Sep3 = new javax.swing.JSeparator();
        locationSP = new javax.swing.JScrollPane();
        locationTable = new javax.swing.JTable();

        setMaximumSize(new java.awt.Dimension(600, 420));
        setMinimumSize(new java.awt.Dimension(600, 420));
        setPreferredSize(new java.awt.Dimension(600, 420));

        customerNoL.setText("Kunde nr.:");
        customerNoL.setName("customerNoL"); // NOI18N

        nameL.setText("Navn:");
        nameL.setName("nameL"); // NOI18N

        addressL.setText("Adresse:");
        addressL.setName("addressL"); // NOI18N

        zipCodeL.setText("Post nr.:");
        zipCodeL.setName("zipCodeL"); // NOI18N

        cityL.setText("By:");
        cityL.setName("cityL"); // NOI18N

        cvrL.setText("CVR:");
        cvrL.setName("cvrL"); // NOI18N

        phoneNoL.setText("Tlf nr.:");
        phoneNoL.setName("phoneNoL"); // NOI18N

        customerNoTF.setEnabled(false);
        customerNoTF.setMaximumSize(new java.awt.Dimension(200, 20));
        customerNoTF.setMinimumSize(new java.awt.Dimension(200, 20));
        customerNoTF.setName("customerNoTF"); // NOI18N
        customerNoTF.setNextFocusableComponent(nameTF);
        customerNoTF.setPreferredSize(new java.awt.Dimension(200, 20));

        nameTF.setEnabled(false);
        nameTF.setMaximumSize(new java.awt.Dimension(200, 20));
        nameTF.setMinimumSize(new java.awt.Dimension(200, 20));
        nameTF.setName("nameTF"); // NOI18N
        nameTF.setNextFocusableComponent(addressTF);
        nameTF.setPreferredSize(new java.awt.Dimension(200, 20));

        addressTF.setEnabled(false);
        addressTF.setMaximumSize(new java.awt.Dimension(200, 20));
        addressTF.setMinimumSize(new java.awt.Dimension(200, 20));
        addressTF.setName("addressTF"); // NOI18N
        addressTF.setNextFocusableComponent(zipCodeTF);
        addressTF.setPreferredSize(new java.awt.Dimension(200, 20));

        zipCodeTF.setEnabled(false);
        zipCodeTF.setMaximumSize(new java.awt.Dimension(200, 20));
        zipCodeTF.setMinimumSize(new java.awt.Dimension(200, 20));
        zipCodeTF.setName("zipCodeTF"); // NOI18N
        zipCodeTF.setNextFocusableComponent(cityTF);
        zipCodeTF.setPreferredSize(new java.awt.Dimension(200, 20));

        cityTF.setEnabled(false);
        cityTF.setMaximumSize(new java.awt.Dimension(200, 20));
        cityTF.setMinimumSize(new java.awt.Dimension(200, 20));
        cityTF.setName("cityTF"); // NOI18N
        cityTF.setNextFocusableComponent(cvrTF);
        cityTF.setPreferredSize(new java.awt.Dimension(200, 20));

        cvrTF.setEnabled(false);
        cvrTF.setMaximumSize(new java.awt.Dimension(200, 20));
        cvrTF.setMinimumSize(new java.awt.Dimension(200, 20));
        cvrTF.setName("cvrTF"); // NOI18N
        cvrTF.setNextFocusableComponent(phoneNoTF);
        cvrTF.setPreferredSize(new java.awt.Dimension(200, 20));

        phoneNoTF.setEnabled(false);
        phoneNoTF.setMaximumSize(new java.awt.Dimension(200, 20));
        phoneNoTF.setMinimumSize(new java.awt.Dimension(200, 20));
        phoneNoTF.setName("phoneNoTF"); // NOI18N
        phoneNoTF.setNextFocusableComponent(addLocationB);
        phoneNoTF.setPreferredSize(new java.awt.Dimension(200, 20));

        searchTF.setMaximumSize(new java.awt.Dimension(200, 20));
        searchTF.setMinimumSize(new java.awt.Dimension(200, 20));
        searchTF.setName("searchTF"); // NOI18N
        searchTF.setPreferredSize(new java.awt.Dimension(200, 20));

        searchB.setText("Søg");
        searchB.setName("searchB"); // NOI18N
        searchB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBActionPerformed(evt);
            }
        });

        addLocationB.setText("Tilføj");
        addLocationB.setEnabled(false);
        addLocationB.setName("addLocationB"); // NOI18N
        addLocationB.setNextFocusableComponent(removeLocationB);
        addLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addLocationBActionPerformed(evt);
            }
        });

        removeLocationB.setText("Fjern");
        removeLocationB.setEnabled(false);
        removeLocationB.setName("removeLocationB"); // NOI18N
        removeLocationB.setNextFocusableComponent(cancelB);
        removeLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeLocationBActionPerformed(evt);
            }
        });

        primaryLocationB.setText("Primær");
        primaryLocationB.setEnabled(false);
        primaryLocationB.setName("primaryLocationB"); // NOI18N
        primaryLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                primaryLocationBActionPerformed(evt);
            }
        });

        showLocationB.setText("Vis");
        showLocationB.setEnabled(false);
        showLocationB.setName("showLocationB"); // NOI18N
        showLocationB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showLocationBActionPerformed(evt);
            }
        });

        showAllB.setText("Vis Alle");
        showAllB.setName("showAllB"); // NOI18N
        showAllB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllBActionPerformed(evt);
            }
        });

        newB.setText("Ny");
        newB.setName("newB"); // NOI18N
        newB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newBActionPerformed(evt);
            }
        });

        editB.setText("Rediger");
        editB.setEnabled(false);
        editB.setName("editB"); // NOI18N
        editB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editBActionPerformed(evt);
            }
        });

        deleteB.setText("Slet");
        deleteB.setEnabled(false);
        deleteB.setName("deleteB"); // NOI18N
        deleteB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBActionPerformed(evt);
            }
        });

        cancelB.setText("Annullér");
        cancelB.setEnabled(false);
        cancelB.setName("cancelB"); // NOI18N
        cancelB.setNextFocusableComponent(saveB);
        cancelB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBActionPerformed(evt);
            }
        });

        saveB.setText("Gem");
        saveB.setEnabled(false);
        saveB.setName("saveB"); // NOI18N
        saveB.setNextFocusableComponent(customerNoTF);
        saveB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBActionPerformed(evt);
            }
        });

        Sep1.setName("Sep1"); // NOI18N

        Sep2.setName("Sep2"); // NOI18N

        Sep3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        Sep3.setName("Sep3"); // NOI18N
        Sep3.setPreferredSize(new java.awt.Dimension(5, 23));

        locationSP.setName("locationSP"); // NOI18N

        locationTable.setModel(new Views.TableModels.LocationTableModel());
        locationTable.setEnabled(false);
        locationTable.setName("locationTable"); // NOI18N
        locationSP.setViewportView(locationTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nameL)
                            .addComponent(customerNoL)
                            .addComponent(addressL)
                            .addComponent(zipCodeL)
                            .addComponent(cityL)
                            .addComponent(cvrL)
                            .addComponent(phoneNoL))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addressTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(zipCodeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cityTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cvrTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(customerNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(phoneNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(Sep1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(searchTF, 0, 0, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchB))
                            .addComponent(locationSP, 0, 0, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addLocationB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeLocationB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(primaryLocationB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(showLocationB))))
                    .addComponent(Sep2, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(showAllB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 187, Short.MAX_VALUE)
                        .addComponent(cancelB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveB)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(searchB))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Sep1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(locationSP, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(showLocationB)
                            .addComponent(primaryLocationB)
                            .addComponent(removeLocationB)
                            .addComponent(addLocationB)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(customerNoL)
                            .addComponent(customerNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nameL)
                            .addComponent(nameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addressL)
                            .addComponent(addressTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(zipCodeL)
                            .addComponent(zipCodeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cityL)
                            .addComponent(cityTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cvrL)
                            .addComponent(cvrTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(phoneNoL)
                            .addComponent(phoneNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(67, 67, 67)
                .addComponent(Sep2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(showAllB)
                    .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(newB)
                        .addComponent(editB)
                        .addComponent(saveB)
                        .addComponent(cancelB)
                        .addComponent(deleteB)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	private void newBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_newBActionPerformed
	{//GEN-HEADEREND:event_newBActionPerformed
		clearComponents();
		changeComponentState(true);
		newOne = new Customer();
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_newBActionPerformed

	private void editBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editBActionPerformed
	{//GEN-HEADEREND:event_editBActionPerformed
		changeComponentState(true);
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_editBActionPerformed

	private void deleteBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBActionPerformed
		if(confirmDeletion("kunden"))
		{
			controller.delete(chosenOne.getCustomerNo());
			clearComponents();
		}
	}//GEN-LAST:event_deleteBActionPerformed

	private void cancelBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelBActionPerformed
	{//GEN-HEADEREND:event_cancelBActionPerformed
		clearComponents();
		changeComponentState(false);
	}//GEN-LAST:event_cancelBActionPerformed

	private void saveBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveBActionPerformed
	{//GEN-HEADEREND:event_saveBActionPerformed
		Customer theOne = getTheOne();

		try
		{
			theOne.setData(nameTF.getText(), cvrTF.getText(), phoneNoTF.getText());
			
			if(chosenOne == null)
			{
				controller.insert(theOne);
			}
			else
			{
				controller.update(theOne, "customerNo = "+theOne.getCustomerNo());
			}
		}
		catch (EmptyValueException e)
		{
			informAboutEmptyFields();
			return;
		}
		catch (NumberFormatException e)
		{
			informAboutWrongInput("CVR, telefonnummer og postnummer skal være heltal.", "Forkert information");
			return;
		}

		informSavingWentOk("Kunden");
		clearComponents();
		changeComponentState(false);
		newB.setEnabled(true);
	}//GEN-LAST:event_saveBActionPerformed

	private void searchBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_searchBActionPerformed
	{//GEN-HEADEREND:event_searchBActionPerformed
		tabbedPane.search(TabIndex.Customers, searchTF.getText());
	}//GEN-LAST:event_searchBActionPerformed

	private void showAllBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showAllBActionPerformed
	{//GEN-HEADEREND:event_showAllBActionPerformed
		tabbedPane.search(TabIndex.Customers, "*");
	}//GEN-LAST:event_showAllBActionPerformed
	
	private void addLocationBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addLocationBActionPerformed
	{//GEN-HEADEREND:event_addLocationBActionPerformed
		if(picker == null)
		{
			picker = createPicker(TabIndex.Locations);
			picker.addWindowListener(this);
			picker.setVisible(true);
		}
	}//GEN-LAST:event_addLocationBActionPerformed

	private void removeLocationBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_removeLocationBActionPerformed
	{//GEN-HEADEREND:event_removeLocationBActionPerformed
		int locationNo = Integer.parseInt(locationTable.getValueAt(locationTable.getSelectedRow(), 0).toString());
		Location location = new LocationController().find(locationNo);
		removeLocation(location);
	}//GEN-LAST:event_removeLocationBActionPerformed

	private void primaryLocationBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_primaryLocationBActionPerformed
	{//GEN-HEADEREND:event_primaryLocationBActionPerformed
		int locationNo = Integer.parseInt(locationTable.getValueAt(locationTable.getSelectedRow(), 0).toString());
		Location location = new LocationController().find(locationNo);
		
		if(chosenOne != null)
		{
			chosenOne.setLocation(location);
		}
		else
		{
			newOne.setLocation(location);
		}
		
		populateWithPrimaryLocation(location);
	}//GEN-LAST:event_primaryLocationBActionPerformed

	private void showLocationBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showLocationBActionPerformed
	{//GEN-HEADEREND:event_showLocationBActionPerformed
		int locationNo = Integer.parseInt(locationTable.getValueAt(locationTable.getSelectedRow(), 0).toString());
		tabbedPane.switchTabWithNo(TabIndex.Locations, locationNo);
	}//GEN-LAST:event_showLocationBActionPerformed

	private void addLocation(Location location)
	{
		Customer theOne = getTheOne();

		if(theOne.getLocations().isEmpty())
		{
			theOne.setLocation(location);
			populateWithPrimaryLocation(location);
		}
		
		theOne.addLocation(location);
		refreshTable(theOne.getLocations());
	}

	private void removeLocation(Location location)
	{
		Customer theOne = getTheOne();
		
		if(theOne.getLocation().getLocationNo() == location.getLocationNo())
		{
			JOptionPane.showMessageDialog(this, "Det er ikke muligt at slette denne lokation, da den er primær for firmaet. Vælg først en anden lokation som primær lokation.", "Lokationen kan ikke slettes", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			theOne.removeLocation(location);
		}
		refreshTable(theOne.getLocations());
	}

	private void refreshTable(ArrayList<Location> locations)
	{
		locationTable.setModel(new LocationTableModel(locations));
	}

	private void populateWithPrimaryLocation(Location location)
	{
		addressTF.setText(location.getAddress());
		zipCodeTF.setText(String.valueOf(location.getZipCode()));
		cityTF.setText(location.getCity());
	}

	protected void changeComponentState(boolean state)
	{
		nameTF.setEnabled(state);
		cvrTF.setEnabled(state);
		phoneNoTF.setEnabled(state);
		locationTable.setEnabled(state);
		addLocationB.setEnabled(state);
		cancelB.setEnabled(state);
		saveB.setEnabled(state);
		addLocationB.setEnabled(state);
		setUnsavedState(state);
	}

	protected void populateFields()
	{
		customerNoTF.setText(String.valueOf(chosenOne.getCustomerNo()));
		nameTF.setText(String.valueOf(chosenOne.getName()));
		addressTF.setText(chosenOne.getLocation().getAddress());
		cvrTF.setText(String.valueOf(chosenOne.getCvr()));
		zipCodeTF.setText(String.valueOf(chosenOne.getLocation().getZipCode()));
		cityTF.setText(chosenOne.getLocation().getCity());
		phoneNoTF.setText(String.valueOf(chosenOne.getPhoneNo()));
		locationTable.setModel(new LocationTableModel(chosenOne.getLocations()));
		newB.setEnabled(true);
		editB.setEnabled(true);
		deleteB.setEnabled(true);
	}

	protected void clearComponents()
	{
		chosenOne = null;
		newOne = null;
		customerNoTF.setText("");
		nameTF.setText("");
		addressTF.setText("");
		zipCodeTF.setText("");
		cityTF.setText("");
		cvrTF.setText("");
		phoneNoTF.setText("");
		locationTable.setModel(new LocationTableModel());
		newB.setEnabled(true);
		editB.setEnabled(false);
		deleteB.setEnabled(false);
	}

	protected void showNo(int no)
	{
		chosenOne = controller.find(no);
		chosenOne.fetchSubItems();
		populateFields();
	}

	public void valueChanged(ListSelectionEvent e)
	{
		if(locationTable.getSelectedRowCount() == 1)
		{
			removeLocationB.setEnabled(true);
			primaryLocationB.setEnabled(true);
			showLocationB.setEnabled(true);
		}
		else
		{
			removeLocationB.setEnabled(false);
			primaryLocationB.setEnabled(false);
			showLocationB.setEnabled(false);
		}
	}

	public void windowClosing(java.awt.event.WindowEvent e)
	{
		if(picker != null)
		{
			Location location = (Location)picker.getTheChosenOne();
			if(location != null)
			{
				this.addLocation(location);
			}
			this.releasePicker();
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator Sep1;
    private javax.swing.JSeparator Sep2;
    private javax.swing.JSeparator Sep3;
    private javax.swing.JButton addLocationB;
    private javax.swing.JLabel addressL;
    private javax.swing.JTextField addressTF;
    private javax.swing.JButton cancelB;
    private javax.swing.JLabel cityL;
    private javax.swing.JTextField cityTF;
    private javax.swing.JLabel customerNoL;
    private javax.swing.JTextField customerNoTF;
    private javax.swing.JLabel cvrL;
    private javax.swing.JTextField cvrTF;
    private javax.swing.JButton deleteB;
    private javax.swing.JButton editB;
    private javax.swing.JScrollPane locationSP;
    private javax.swing.JTable locationTable;
    private javax.swing.JLabel nameL;
    private javax.swing.JTextField nameTF;
    private javax.swing.JButton newB;
    private javax.swing.JLabel phoneNoL;
    private javax.swing.JTextField phoneNoTF;
    private javax.swing.JButton primaryLocationB;
    private javax.swing.JButton removeLocationB;
    private javax.swing.JButton saveB;
    private javax.swing.JButton searchB;
    private javax.swing.JTextField searchTF;
    private javax.swing.JButton showAllB;
    private javax.swing.JButton showLocationB;
    private javax.swing.JLabel zipCodeL;
    private javax.swing.JTextField zipCodeTF;
    // End of variables declaration//GEN-END:variables
}